package com.own.business.resource.business.util;

import com.own.business.resource.entity.vo.FileRecordVo;
import com.own.business.resource.mapper.FileRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * FileSearchUtil
 *
 * @author chenxueli
 * @date 2023-07-07 14:30:13
 */
@Slf4j
@Component
public class ListFileUtil {

    @Resource
    private FileRecordMapper fileRecordMapper;

    /**
     * 将文件查询对象赋予文件内容
     *
     * @param list   列表结果
     * @param mapper 获取文件字段的方法
     * @param set    设置文件列表的方法
     * @param <T>    文件对象类型
     * @author chenxueli
     * @date 2021/9/16 16:54:00
     */
    public <T> void build(Collection<T> list, Function<? super T, String> mapper, BiConsumer<T, List<FileRecordVo>> set) {
        if (list.isEmpty()) {
            return;
        }
        // 获取所有的文件id信息
        var fileIdList = list.parallelStream()
                .map(mapper)
                .filter(StringUtils::isNotEmpty)
                .flatMap(item -> Arrays.stream(item.split(",")))
                .filter(StringUtils::isNotEmpty)
                .map(Long::valueOf)
                .collect(Collectors.toSet());
        if (fileIdList.isEmpty()) {
            return;
        }
        var fileList = fileRecordMapper.selectBatchIds(fileIdList);
        var fileMap = fileList.parallelStream().map(FileRecordVo::new).collect(Collectors.toMap(FileRecordVo::getId, i -> i));
        list.stream()
                .filter(item -> StringUtils.isNotEmpty(mapper.apply(item)))
                .forEach(item -> {
                    var itemFileList = Arrays.stream(mapper.apply(item).split(","))
                            .map(Long::valueOf)
                            .filter(fileMap::containsKey)
                            .map(fileMap::get)
                            .collect(Collectors.toList());
                    set.accept(item, itemFileList);
                });
    }

    /**
     * 将文件查询对象赋予文件内容
     *
     * @param entity 列表结果
     * @param mapper 获取文件字段的方法
     * @param set    设置文件列表的方法
     * @param <T>    文件对象类型
     * @author chenxueli
     * @date 2021/9/16 16:55:00
     */
    public <T> void build(T entity, Function<? super T, String> mapper, BiConsumer<T, List<FileRecordVo>> set) {
        if (entity == null) {
            return;
        }
        var fileIds = mapper.apply(entity);
        if (StringUtils.isEmpty(fileIds)) {
            return;
        }
        var fileIdList = Arrays.stream(fileIds.split(",")).map(Long::valueOf).collect(Collectors.toSet());
        var fileList = fileRecordMapper.selectBatchIds(fileIdList).stream().map(FileRecordVo::new).collect(Collectors.toList());
        set.accept(entity, fileList);
    }

}
